﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ShowFileContent : System.Web.UI.Page
{

	protected void Page_Load(object sender, EventArgs e)
	{
		int nFileContentID = 0;

		try
		{
			nFileContentID = Convert.ToInt32(Request["FileContentID"]);
			ShowContent(nFileContentID);
		}
		catch
		{
			nFileContentID = 0;
			TonyNet4V1.ShowMessageAlert.ShowError(Page, "变量FileContentID没有值，检查！");
		}
	}

	/// <summary>
	/// 根据文件内容ID显示文件内容
	/// </summary>
	/// <![CDATA[2014-8-20 10:01:59]]>
	/// <param name="nFileContentID"></param>
	private void ShowContent(int nFileContentID)
	{
		DLL.FileContent.Component clsFileContentComp = new DLL.FileContent.Component(Page);
		DLL.FileContent.Entity clsFileContent = new DLL.FileContent.Entity();
		byte[] bFileContent;
		string[] strArrShow;
		Boolean bShow;			// 上传的文件是否直接显示，true直接显示

		strArrShow = new string[2];
		bShow = false;

		strArrShow[0] = "text/html";
		strArrShow[1] = "message/rfc822";

		clsFileContent = clsFileContentComp.GetOne(nFileContentID);

		if (clsFileContent != null)
		{

			bFileContent = clsFileContent.Content;
			Response.Clear();
			if (clsFileContent.Type == "application/octet-stream")
			{
				clsFileContent.Type = "message/rfc822";
			}
			Response.AddHeader("Content-Type", clsFileContent.Type);


			for (int i = 0; i < strArrShow.Length; i++)
			{
				if (clsFileContent.Type == strArrShow[i])
				{
					bShow = true;
					break;
				}
			}
			if (bShow)
			{
				Response.AddHeader("Content-Disposition", "inline;");
			}
			else
			{
				Response.AddHeader("Content-Disposition", "inline;");
				HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + TonyNet4V1.CommonFunction.UTF_FileName(clsFileContent.FileName) + "\";");
			}
			//Response.AddHeader("Content-Disposition", "inline;");

			Response.BinaryWrite(bFileContent);
			Response.Flush();
			Response.End();
		}
		else
		{
			TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "错误！根据文件内容ID没有查到记录" + System.IO.Path.GetFileName(Request.Path).ToString());
		}
	}
}